/*
 * Powered By [Frank-Liz-Lee]
 * Copyright(C) 2012-2016 Liz Company
 * All rights reserved.
 */

package com.linln.modules.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linln.common.constant.StatusConst;
import com.linln.common.enums.StatusEnum;
import com.linln.modules.system.mapper.DeptMapper;
import com.linln.modules.system.domain.Dept;
import java.util.List;

import com.linln.modules.system.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional(rollbackFor = Exception.class)
@Service
public class DeptServiceImpl  extends GenericService<Dept, Long>  implements DeptService {

    private DeptMapper deptMapper;

    @Autowired
    public void setDeptMapper(DeptMapper deptMapper) {
        this.deptMapper = deptMapper;
    }

    /**
     * 通过主键查询实体对象
     * @param primaryKey
     * @return
     * @
     */
    public Dept getByPK(Long primaryKey)  {
        return deptMapper.getByPK(primaryKey);
    }

    /**
     * 查询所有记录
     * @return
     * @
     */
    public List<Dept> list()  {
        return deptMapper.list();
    }

    /**
     * 根据查询条件查询所有记录
     * @return
     * @
     */
    public List<Dept> listByProperty(Dept dept) {
        return deptMapper.listByProperty(dept);
    }


    /**
     * 根据主键删除记录
     * @param primaryKey
     * @return
     * @
     */
    public int deleteByPK(Long primaryKey)  {
        return deptMapper.deleteByPK(primaryKey);
    }

    /**
     * 根据多个主键删除记录
     * @param primaryKeys
     * @
     */
    public int deleteByPKeys(List<Long> primaryKeys)  {
        return deptMapper.deleteByPKeys(primaryKeys);
    }

    /**
     * 根据传入参数删除记录
     * @param dept
     * @return
     * @
     */
    public int deleteByProperty(Dept dept)  {
        return deptMapper.deleteByProperty(dept);
    }

    /**
     * 保存记录
     * @param dept
     * @return
     * @
     */
    public int save(Dept dept)  {
        return deptMapper.save(dept);
    }

    /**
     * 更新记录
     * @param dept
     * @return
     * @
     */
    public int update(Dept dept) {
        return deptMapper.update(dept);
    }

    /**
     * 根据条件查询记录条数
     * @param dept
     * @return
     * @
     */
    public int findByCount(Dept dept)  {
        return deptMapper.findByCount(dept);
    }

    /**
     * 根据查询条件查询分页记录
     * @return
     */
    public PageInfo<Dept> findByPage(Integer pageNum, Integer pageSize, Dept dept) {
        PageHelper.startPage(pageNum,pageSize);
        List<Dept> deptList = deptMapper.listByProperty(dept);
        if(null == deptList || deptList.size() == 0){
            return new Page<Dept>().toPageInfo();
        }
        Page<Dept> page =  (Page<Dept>)deptList;
            return page.toPageInfo();
    }

    @Override
    public Integer getSortMax(Long pid) {
        return deptMapper.getSortMax(pid, StatusConst.DELETE);
    }

    @Override
    public boolean updateStatus(StatusEnum statusEnum, List<Long> ids) {
        int result =  deptMapper.updateStatus(statusEnum.getCode(),ids);
        return result > 0;
    }

    /**
     * 根据父级部门ID获取本级全部部门
     * @param pid
     * @return
     */
    @Override
    public List<Dept> getListByPid(Long pid) {
//        Sort sort = new Sort(Sort.Direction.ASC, "sort");
//        return deptRepository.findByPidAndIdNot(sort, pid, notId);
        Dept temp = new Dept();
        temp.setPid(pid);
        return deptMapper.listByProperty(temp);
    }

}
